<%--
  Created by IntelliJ IDEA.
  User: LMY
  Date: 2025/6/21
  Time: 21:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
      <title>计算器</title>
    <script type="text/javascript">

      <%--动态注册表单提交事件--%>
      window.onload = function() {
        //获取form的dom对象
        var form = document.getElementById("form");
        //给form绑定提交事件
        form.onsubmit = function () {
          //非空校验
          if (form.num1 == "" || form.num2 == "") {
            alert("请输入num1和num2");
            return false;
          }
          //正则表达式校验
          if (!regCheck(form.num1.value, form.num2.value)) {
            alert("必须输入整数");
            form.num1.value = "";
            form.num2.value = "";
            return false;
          }
          return true;
        }

        function regCheck (num1, num2) {
          const regex = /^-?\d+$/;
          if (regex.test(num1) && regex.test(num2)){
            return true;
          } else {
            return false;
          }
        }
      }
    </script>
  </head>

  <body>
  <form action="res.jsp" id="form">
    <h1>jsp版本-计算器</h1>
  num1: <input type="text" id="num1" name="num1"><br/>
  num2: <input type="text" id="num2" name="num2"><br/>
  运算符号:
      <select name="symbol">
        <option selected="selected" value="+">+</option>
        <option value="-">-</option>
        <option value="*">*</option>
        <option value="/">/</option>
      </select><br/>
  <input type="submit" value="计算">
  </form>
  </body>
</html>
